<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>
<style type="text/css">
	
</style>
<div class="page-header">
	<h1><?=$schoolInfo['school_name']?> 校区管理</h1>
</div>

<div class="col-xs-12">
	<a href="#modal-campus" class="btn btn-primary show-modal" data-toggle="modal" data-id="" data-class="">
		<i class="ace-icon fa fa-table"></i> 添加校区
	</a>
	<table id="dynamic-table" class="table table-striped table-bordered table-hover" style="margin-top:10px">
		<thead>
			<tr>
				<th>校区ID</th>
				<th>校区名称</th>
				<th>创建时间</th>
				<th>操作</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
</div>

<!-- 校区弹窗 -->
<div id="modal-campus" class="modal fade">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header no-padding">
				<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button>
					<span id="modal-name"></span>
				</div>
			</div>
			<div class="modal-body no-padding">
				<div class="row">
					<div class="col-xs-12">
						<form class="form-horizontal mt10" id="campus-form">
							<input type="hidden" name="id" />
							<div class="form-group">
								<label class="col-sm-3 control-label no-padding-right">校区名称：</label>
								<div class="col-sm-6">
									<input type="text" class="form-control" name="campus_name" placeholder="请填写校区名称" />
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
			<div class="modal-footer no-margin-top" >
				<a href="javascript:;" class="btn btn-sm btn-primary" id="save-campus">
					<i class="ace-icon fa fa-check"></i> 保存
				</a>
			</div>
		</div>
	</div>
</div>

<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	var table = "";

	$(function(){
		var form = $("#campus-form");

		// 显示弹窗
		$("body").on("click", ".show-modal", function(){
			var id = $(this).data("id");
			var name = $(this).data("name");
			form.find("[name=id]").val(id);
			form.find("[name=campus_name]").val(name);
			// 修改弹窗名称
			$("#modal-name").text(id ? '修改校区' : '新增校区');
		})

		// 提交表单
		$("#save-campus").click(function(){
			form.submit();
		})

		var submitUrl = "<?=admin_url('admin/save_campus_action')?>/<?=$schoolInfo['id']?>";
		form_submit(form, submitUrl, function(res) {
			tips_alert(res.msg, res.success, function(){
				if(res.success) window.location.reload();
			});
		})

		// 班级表格
		table = $('#dynamic-table').dt({
			"ajaxUrl": '<?=admin_url('admin/get_campus_list')?>/<?=$schoolInfo['id']?>',
			"columns": [
	            {"data": "id"},
	            {"data": "campus_name"},
	            {"data": "createtime"},
	            {"data": function(ret){
	            	var action = "";
	            	action += '<a class="show-modal green" href="#modal-campus" data-toggle="modal" data-id="'+ ret.id +'" data-name="'+ ret.campus_name +'" ><i class="fa fa-edit"></i> 修改</a>';
	            	action += '<a href="javascript:;" class="delete-campus red ml10" data-id="'+ ret.id +'"><i class="fa fa-times"></i> 删除</a>';
	                return action;
	            }},
			]
		});

		// 删除用户
		$("body").on("click", ".delete-campus", function(){
			var id = $(this).data("id");
			layer.confirm('确定删除该校区吗？将同时删除校区对应的所有基础数据', function(index){
				layer.close(index);

				// 提交数据
				var actionUrl = "<?=admin_url('admin/delete_campus_action')?>";
				var actionData = {"id": id};
				ajax_get(actionUrl, actionData, function(){
					table.draw(false);
				});
			});
		})
		
	})
</script>